﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructure.Model.DI;
using Model.EntityModel.SysModel;
using Repository.IRepository.ISysRepository;
using Repository.Repository.SysRepository;
using Service.IService.ISysService;

namespace Service.Service.SysService
{
    [AppService(ServiceType = typeof(ILocationService), ServiceLifetime = LifeTime.Scoped)]
    public class LocationService : ILocationService
    {
        private readonly ILocationRepository _locationRepository;

        public LocationService(ILocationRepository locationRepository)
        {
            _locationRepository = locationRepository;
        }
        public List<LocationMaster> GetAll()
        {
            try
            {
                return _locationRepository.GetAllAsync().GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public string AddLocation(LocationMaster location)
        {
            try
            {
                location.Id = Guid.NewGuid().ToString();
                location.CreatedAt = DateTime.Now;

                var success = _locationRepository.InsertAsync(location).GetAwaiter().GetResult();
                return (success > 0) ? location.Id : null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool UpdateLocation(LocationMaster location)
        {
            try
            {
        
                return _locationRepository.UpdateAsync(location).GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool DeleteLocation(string locationId)
        {
            try
            {
                return _locationRepository.DeleteAsync(locationId).GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public LocationMaster GetLocationById(string locationId)
        {
            try
            {
                return _locationRepository.GetByIdAsync(locationId).GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<LocationMaster> GetAllLocations()
        {
            try
            {
                return _locationRepository.GetAllAsync().GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<LocationMaster> GetLocationsByArea(string area)
        {
            try
            {
                return _locationRepository.GetLocationsByAreaAsync(area).GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool UpdateLocationArea(string locationId, string newArea)
        {
            try
            {
                return _locationRepository.UpdateLocationAreaAsync(locationId, newArea).GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<LocationMaster> SearchLocations(string keyword)
        {
            try
            {
                return _locationRepository.SearchLocationsAsync(keyword).GetAwaiter().GetResult();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
